package com.cnh.o2o.interceptor.shopadmin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.cnh.o2o.entity.Shop;
/**
 * 对店家管理操作进行验证的拦截器
 * @author cnh
 *
 */
public class ShopPermissionInterceptor extends HandlerInterceptorAdapter {
	/**
	 * 主要做事前拦截,即用户操作发生前,改写preHandle里的逻辑,进行用户操作权限的拦截
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		Shop currentShop = (Shop) request.getSession().getAttribute(
				"currentShop");
		//从session中获取当前用户可操作的店铺列表
		@SuppressWarnings("unchecked")
		List<Shop> shopList = (List<Shop>) request.getSession().getAttribute(
				"shopList");
		if (currentShop != null && shopList != null) {
			//遍历可操作的店铺列表
			for (Shop shop : shopList) {
				//如果当前店铺在可操作的列表里则返回true,进行接下来的用户操作
				if (shop.getShopId() == currentShop.getShopId()) {
					return true;
				}
			}
		}
		return false;
	}
}
